package org.feng.common;

import lombok.SneakyThrows;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;

/**
 * TODO 类的描述
 *
 * @version V1.0
 * @author: fengjinsong
 * @date: 2022年07月01日 17时36分
 */
public class CustomBeanNameGenerator extends AnnotationBeanNameGenerator {
    public static final CustomBeanNameGenerator INSTANCE = new CustomBeanNameGenerator();

    public CustomBeanNameGenerator() {
    }

    @SneakyThrows
    @Override
    protected String buildDefaultBeanName(BeanDefinition definition) {
        String beanClassName = definition.getBeanClassName();
        final Class<?> aClass = Class.forName(beanClassName);
        if (aClass.isAnnotationPresent(BeanBusiness.class)) {
            BeanBusiness beanBusiness = aClass.getAnnotation(BeanBusiness.class);
            // 后缀
            String suffix = "BeanBusiness";
            BusinessOneEnum one = beanBusiness.one();
            BusinessTwoEnum two = beanBusiness.two();
            // 拼接beanName
            return String.join("_", one.getName(), two.getName(), suffix);
        }
        return super.buildDefaultBeanName(definition);
    }
}
